#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <sys/time.h>
 
#include "data_type.h"
#include "cube.h"
#include "cube_define.h"
#include "cube_record.h"
<<<<<<< HEAD
=======
#include "user_define.h"
>>>>>>> 8376f8f7f42a87a3cd13ea14ef218ae5e31f317b
#include "file_struct.h"
// add para lib_include
int instance_label_init(void * sub_proc, void * para)
{
	int ret;
	// add yorself's module init func here
	return 0;
}
int instance_label_start(void * sub_proc, void * para)
{
	int ret;
	void * recv_msg;
	int type;
	int subtype;
	// add yorself's module exec func here
	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
			message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}
		if((type==TYPE(MESSAGE))&&(subtype==SUBTYPE(MESSAGE,CONN_ACKI)))
		{
			ret=proc_conn_clientinfo(sub_proc,recv_msg);
		}
		else if((type==TYPE(MESSAGE))&&(subtype==SUBTYPE(MESSAGE,CONN_SYNI)))
		{
			ret=proc_conn_serverinfo(sub_proc,recv_msg);
		}
		else
		{
			ret=proc_conn_instanceattach(sub_proc,recv_msg);
		}
	}
	return 0;
}

int proc_conn_clientinfo(void * sub_proc,void * recv_msg)
{
	int ret;
	RECORD(MESSAGE,INSTANCE_INFO) * instance_info;
	RECORD(MESSAGE,INSTANCE_INFO) * lib_info;
	RECORD(MESSAGE,CONN_ACKI) * conn_acki;
	BYTE conn_uuid[DIGEST_SIZE];
	BYTE comp_uuid[DIGEST_SIZE];
	
	ret=message_get_record(recv_msg,&conn_acki,0);
	if(ret<0)
		return ret;

    instance_info = Talloc0(sizeof(*instance_info));
    if(instance_info==NULL)
        return -ENOMEM;
    
    Memcpy(instance_info->node_uuid,conn_acki->uuid,DIGEST_SIZE);
    Memcpy(instance_info->proc_name,conn_acki->client_process,DIGEST_SIZE);
    Memset(instance_info->user_name,0,DIGEST_SIZE);

	comp_proc_uuid(instance_info->node_uuid,instance_info->proc_name,conn_uuid);

	DB_RECORD * db_record;

	db_record=memdb_get_first(TYPE_PAIR(MESSAGE,INSTANCE_INFO));
	while(db_record!=NULL)
	{
		lib_info = db_record->record;
		if(lib_info == NULL)
			return -EINVAL;
	//	Memcpy(addr_label->proc_uuid,conn_uuid,DIGEST_SIZE);

        if(Memcmp(lib_info->node_uuid,instance_info->node_uuid,DIGEST_SIZE)==0)
            if(Strncmp(lib_info->proc_name,instance_info->proc_name,DIGEST_SIZE)==0)
            {
                if(lib_info->user_name[0]!=0)
                {
                    memdb_remove_record(db_record);
                    break;
                }
            }
	    db_record=memdb_get_next(TYPE_PAIR(MESSAGE,INSTANCE_INFO));
	}
	
	memdb_store(instance_info,TYPE_PAIR(MESSAGE,INSTANCE_INFO),NULL);
	ret=ex_module_sendmsg(sub_proc,recv_msg);
	return ret;
}

int proc_conn_serverinfo(void * sub_proc,void * recv_msg)
{
	int ret;
	RECORD(MESSAGE,INSTANCE_INFO) * instance_info;
	RECORD(MESSAGE,INSTANCE_INFO) * lib_info;
	RECORD(MESSAGE,CONN_SYNI) * conn_syni;
	BYTE conn_uuid[DIGEST_SIZE];
	BYTE comp_uuid[DIGEST_SIZE];
	
	ret=message_get_record(recv_msg,&conn_syni,0);
	if(ret<0)
		return ret;

    instance_info = Talloc0(sizeof(*instance_info));
    if(instance_info==NULL)
        return -ENOMEM;
    
    Memcpy(instance_info->node_uuid,conn_syni->uuid,DIGEST_SIZE);
    Memcpy(instance_info->proc_name,conn_syni->server_name,DIGEST_SIZE);
    Memset(instance_info->user_name,0,DIGEST_SIZE);

	comp_proc_uuid(instance_info->node_uuid,instance_info->proc_name,conn_uuid);

	DB_RECORD * db_record;

	db_record=memdb_get_first(TYPE_PAIR(MESSAGE,INSTANCE_INFO));
	while(db_record!=NULL)
	{
		lib_info = db_record->record;
		if(lib_info == NULL)
			return -EINVAL;
	//	Memcpy(addr_label->proc_uuid,conn_uuid,DIGEST_SIZE);

        if(Memcmp(lib_info->node_uuid,instance_info->node_uuid,DIGEST_SIZE)==0)
            if(Strncmp(lib_info->proc_name,instance_info->proc_name,DIGEST_SIZE)==0)
            {
                if(lib_info->user_name[0]!=0)
                {
                    memdb_remove_record(db_record);
                    break;
                }
            }
	    db_record=memdb_get_next(TYPE_PAIR(MESSAGE,INSTANCE_INFO));
	}
	
	memdb_store(instance_info,TYPE_PAIR(MESSAGE,INSTANCE_INFO),NULL);
	ret=ex_module_sendmsg(sub_proc,recv_msg);
	return ret;
}

int proc_conn_instanceattach(void * sub_proc,void * recv_msg)
{
	int ret=0;
	RECORD(MESSAGE,INSTANCE_INFO) * lib_info;
	MSG_EXPAND * msg_expand;

    BYTE * proc_uuid;
	BYTE comp_uuid[DIGEST_SIZE];

	proc_uuid = message_get_sender(recv_msg);

	DB_RECORD * db_record;

	db_record=memdb_get_first(TYPE_PAIR(MESSAGE,INSTANCE_INFO));
	while(db_record!=NULL)
	{
		lib_info = db_record->record;
		if(lib_info == NULL)
			return -EINVAL;
	//	Memcpy(addr_label->proc_uuid,conn_uuid,DIGEST_SIZE);
	    comp_proc_uuid(lib_info->node_uuid,lib_info->proc_name,comp_uuid);

        if(Memcmp(comp_uuid,proc_uuid,DIGEST_SIZE)==0)
        {
	        ret=message_add_expand_data(recv_msg,TYPE_PAIR(MESSAGE,INSTANCE_INFO),lib_info);
	        if(ret<0)
		          return ret;
            break;
        }
	    db_record=memdb_get_next(TYPE_PAIR(MESSAGE,INSTANCE_INFO));
    }	
	ret=ex_module_sendmsg(sub_proc,recv_msg);
	return ret;
}
